﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;

namespace Ict.DataAccess
{
    /// <summary>
    /// 数据实体类，用于封装记录行，作为业务实体类的基类使用
    /// </summary>
    public class DataEntity
    {
        private Dictionary<string, object> _assignValues = null;
        private DataRow _dataRow = null;

        public DataEntity()
        {
        }

        public DataEntity(DataRow dataRow)
        {
            _dataRow = dataRow;
        }

        public DataRow DataRow
        {
            get 
            {
                return _dataRow; 
            }
            set
            {
                _dataRow = value;
            }
        }

        /// <summary>
        /// Set or Get AssignValues
        /// </summary>
        public Dictionary<string, object> AssignValues
        {
            get
            {
                return _assignValues;
            }
        }

        /// <summary>
        /// 获取或设置指定字段的值，如字段被设置，则返回设置值，否则返回记录行中的值，如既未设新值又未初始化记录行，则抛出错误。
        /// </summary>
        /// <param name="fieldName">字段名称</param>
        /// <returns>字段值</returns>
        public object this[string fieldName]
        {
            get
            {
                object o = null;
                if (_assignValues != null && _assignValues.ContainsKey(fieldName))
                    o = _assignValues[fieldName];
                else if (_dataRow != null)
                    o = _dataRow[fieldName];
                else
                    throw new Exception("DataEntity is not initialized.");

                // 如果值为DBNull，则根据类型进行相应的处理
                if (o == null || o is DBNull)
                {
                    if (_dataRow != null)
                    {
                        Type t = _dataRow.Table.Columns[fieldName].DataType;
                        if (t == typeof(int))
                            o = (int)0;
                        else if (t == typeof(decimal))
                            o = (decimal)0;
                        else if (t == typeof(bool))
                            o = false;
                        else
                            o = null;
                    }
                    else
                        o = null;
                }
                return o;
            }
            set
            {
                if (_assignValues == null)
                    _assignValues = new Dictionary<string, object>();
                _assignValues[fieldName] = value;
            }
        }

        /// <summary>
        /// 将字段值转换为字符串形式
        /// </summary>
        /// <param name="fieldName">字段名</param>
        /// <returns>字段值的字符串形式</returns>
        public string ToString(string fieldName)
        {
            object v = this[fieldName];
            if (v != null && !Convert.IsDBNull(v))
            {
                return v.ToString();
            }
            else
                return "";
        }

        /// <summary>
        /// 将字段值转换为字符串形式，只有支持格式化字符串的decimal/DateTime/int类型才可使用此方法，否则将抛出错误
        /// </summary>
        /// <param name="fieldName">字段名</param>
        /// <param name="format">字符串格式</param>
        /// <returns>字段值的字符串形式</returns>
        public string ToString(string fieldName, string format)
        {
            object v = this[fieldName];
            if (v != null && !Convert.IsDBNull(v))
            {
                if (v is decimal)
                    return ((decimal)v).ToString(format);
                else if (v is DateTime)
                    return ((DateTime)v).ToString(format);
                else if (v is int)
                    return ((int)v).ToString(format);
                else
                    throw new Exception("The field '" + fieldName + "' is not decimal/DateTime/int. ToString(format) is not supported.");
            }
            else
                return "";
        }

    }
}
